package com.lindroid.androiduilearning.recyclerview.fragment;


import android.os.Bundle;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.lindroid.androiduilearning.R;
import com.lindroid.androiduilearning.base.BaseFragment;
import com.lindroid.androiduilearning.recyclerview.DiffCallback;
import com.lindroid.androiduilearning.recyclerview.RecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linyulong
 */
public class DiffFragment extends BaseFragment implements View.OnClickListener {

    private List<String> titles = new ArrayList<>();
    private List<String> oldTitles = new ArrayList<>();

    private RecyclerView rvDiff;
    private RecyclerAdapter adapter;
    private Button btnAdd, btnDelete, btnUpdate;
    private int updatePos = 0;

    public DiffFragment() {
    }

    public static DiffFragment newInstance() {
        Bundle args = new Bundle();
        DiffFragment fragment = new DiffFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = View.inflate(context, R.layout.fragment_diff, null);
        initView(rootView);
        initData();
        return rootView;
    }

    private void initData() {
        for (int i = 0; i < 50; i++) {
            titles.add(new StringBuffer("数据").append(i).toString());
        }
        adapter = new RecyclerAdapter(getActivity(), titles);
        rvDiff.setAdapter(adapter);

    }

    private void initView(View rootView) {
        rvDiff = rootView.findViewById(R.id.rv_diff);
        rvDiff.setLayoutManager(new LinearLayoutManager(context));
        rvDiff.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        btnAdd = rootView.findViewById(R.id.btn_add);
        btnDelete = rootView.findViewById(R.id.btn_delete);
        btnUpdate = rootView.findViewById(R.id.btn_update);
        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add:
                oldTitles.clear();
                oldTitles.addAll(titles);
                titles.add(1, "新数据");
                break;
            case R.id.btn_delete:
                oldTitles.clear();
                oldTitles.addAll(titles);
                titles.remove(4);
                break;
            case R.id.btn_update:
                oldTitles.clear();
                oldTitles.addAll(titles);
                updatePos = updatePos + 2;
                titles.set(updatePos, "更改数据");
                break;
            default:
                break;
        }
        /**
         * 参数二：检测item是否移动
         */
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallback(oldTitles, titles), false);
        diffResult.dispatchUpdatesTo(adapter);

    }


}
